<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserNotificationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_notifications', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('notification_type', 50)->comment('achievement, daily_challenge, encouragement, comeback');
            $table->string('title', 200);
            $table->text('content');
            $table->boolean('is_read')->default(false);
            $table->timestamp('scheduled_at')->nullable()->comment('定时推送时间');
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('created_at')->useCurrent();
            
            // 外键约束
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            
            // 索引
            $table->index(['user_id']);
            $table->index(['is_read']);
            $table->index(['scheduled_at']);
            $table->index(['notification_type']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_notifications');
    }
}